/// @tags: MaxFlow
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>

using namespace std;

namespace BlueQuantum {

typedef long long LL;

int const N = 1e5 + 5, INF = 0x3f3f3f3f;
int const dx[] = {1, -1, 0, 0}, dy[] = {0, 0, -1, 1};

struct Edge {
  int v, nxt, f;
} e[N << 3];

int n, r, c, s, t, e_cnt = 1, cnt;
int heads[N], cur[N], d[N], x[N], y[N], w[N], col[N];
map<LL, int> mp;
queue<int> q;

inline void add(int u, int v, int f) {
  e[++e_cnt].v = v, e[e_cnt].f = f, e[e_cnt].nxt = heads[u], heads[u] = e_cnt;
  e[++e_cnt].v = u, e[e_cnt].f = 0, e[e_cnt].nxt = heads[v], heads[v] = e_cnt;
}

inline bool bfs() {
  memset(d, 0, sizeof(d));
  memcpy(cur, heads, sizeof(cur));
  while (q.size()) q.pop();
  d[s] = 1;
  q.push(s);
  while (q.size()) {
    int x = q.front();
    q.pop();
    for (int i = heads[x], v; i; i = e[i].nxt) {
      if (e[i].f && !d[v = e[i].v]) {
        q.push(v);
        d[v] = d[x] + 1;
        if (v == t) return true;
      }
    }
  }
  return false;
}

int dinic(int x, int flow) {
  if (x == t) return flow;
  int rest = flow, k = 0;
  for (int &i = cur[x], v; i && rest; i = e[i].nxt) {
    if (e[i].f && d[v = e[i].v] == d[x] + 1) {
      k = dinic(v, min(e[i].f, rest));
      if (!k) d[v] = 0;
      e[i].f -= k;
      e[i ^ 1].f += k;
      rest -= k;
    }
  }
  return flow - rest;
}

inline LL id(int i, int j) { return (LL)(i - 1) * (c + 1) + j; }

inline void buildA(int i, int j, int x) {
  for (int k = 0; k < 4; ++k) {
    int tx = dx[k] + i, ty = dy[k] + j;
    map<LL, int>::iterator it;
    if ((it = mp.find(id(tx, ty))) != mp.end()) {
      if (col[it->second] != 2) add(it->second, x, INF);
      else
        add(x, it->second, min(w[it->second], w[x]));
    }
  }
}

inline void buildB(int i, int j, int x) {
  for (int k = 0; k < 4; ++k) {
    int tx = dx[k] + i, ty = dy[k] + j;
    map<LL, int>::iterator it;
    if ((it = mp.find(id(tx, ty))) != mp.end())
      if (col[it->second] != 1) add(x, it->second, INF);
  }
}

inline int main() {
  cin >> c >> r >> n;
  s = n + 1, t = n + 2;
  for (int i = 1; i <= n; ++i) {
    cin >> x[i] >> y[i] >> w[i];
    mp[id(x[i], y[i])] = i;
  }
  for (int i = 1; i <= n; ++i) switch (x[i] % 4) {
      case 1:
        col[i] = (y[i] & 1) ? 1 : 3;
        break;
      case 2:
        col[i] = (y[i] & 1) ? 2 : 4;
        break;
      case 3:
        col[i] = (y[i] & 1) ? 4 : 2;
        break;
      case 0:
        col[i] = (y[i] & 1) ? 3 : 1;
    }
  for (int i = 1; i <= n; ++i) switch (col[i]) {
      case 1:
        buildA(x[i], y[i], i);
        break;
      case 2:
        buildB(x[i], y[i], i);
        break;
      case 3:
        add(s, i, w[i]);
        break;
      case 4:
        add(i, t, w[i]);
    }
  int res = 0;
  while (bfs()) res += dinic(s, INF);
  cout << res;
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("P3756 [CQOI2017] 老C的方块.in", "r", stdin);
  freopen("P3756 [CQOI2017] 老C的方块.out", "w", stdout);
#endif
#endif

  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
